<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../../inc/inc.jsp"></jsp:include>
		<jsp:include page="../auth.jsp"></jsp:include>
		<script type="text/javascript">
		    $(document).ready(function () { 
		    	$("#datagd").datagrid({
			        striped: true, 
			        border: false,
			        loadMsg: '正在加载数据......',
			        fit: true,				//自动大小 
			        url:"info_data", 
			        idField:'id', 
			        singleSelect:true,//是否单选 
			        pagination:true,//分页控件 
			        rownumbers:true,//行号 
			        remoteSort:false,
			        columns:[[{
						field : 'id',
						title : 'ID',
						width : 120,
						hidden : true
					},{
						field : 'user_name',
						title : '用户名',
						halign : 'center',
						width : 120,
						sortable:true,
						formatter : function(value, row, index) {
							return $.formatString('<a href="javascript:void(0)"; onclick="view(\'{0}\')";>{1}</a>',row.id,value);
						}
					},{
						field : 'pwd_bak',
						title : '密码',
						width : 120,
						hidden : true
					},{
						field : 'dept_name',
						title : '所属部门',
						halign : 'center',
						width : 120
					},{
						field : 'dept_id',
						title : '所属部门ID',
						width : 120,
						halign : 'center',
						hidden : true
					},{
						field : 'staff_no',
						title : '员工编号',
						halign : 'center',
						width : 120,
						sortable:true
					},{
						field : 'staff_name',
						title : '员工姓名',
						halign : 'center',
						width : 150,
						sortable:true
					},{
						field : 'sex',
						title : '性别',
						width : 90,
						halign : 'center',
						formatter : function(value, row, index) {
							switch (value) {
							case "0":
								return '男';
							case "1":
								return '女';
							}
						}
					},{
						field : 'birth_date',
						title : '生日',
						halign : 'center',
						width : 150,
						sortable:true
					},{
						field : 'email',
						title : 'Email',
						halign : 'center',
						width : 150
					},{
						field : 'telephone',
						title : '电话',
						halign : 'center',
						width : 120
					},{
						field : 'cellphone',
						title : '手机',
						halign : 'center',
						width : 120
					},{
						field : 'fax',
						title : '传真',
						halign : 'center',
						width : 120
					},{
						field : 'qq_chat',
						title : 'QQ',
						halign : 'center',
						width : 120
					},{
						field : 'card_no',
						title : '证件号码',
						halign : 'center',
						width : 120
					},{
						field : 'create_date',
						title : '创建时间',
						halign : 'center',
						width : 120
					},{
						field : 'last_date',
						title : '最后登录时间',
						halign : 'center',
						width : 120
					},{
						field : 'position',
						title : '职位',
						halign : 'center',
						width : 100
					},{
						field : 'remark',
						title : '备注',
						halign : 'center',
						width : 150
					}]],
			        toolbar: '#toolbar'
			    });
		 
		    	//所属部门
				$('#deptId').combotree({
					url : '${pageContext.request.contextPath}/sys/dept/dept_tree',
					parentField : 'parentId',
					lines : true,
					panelHeight : 'auto',
					width : 200
				});
		    })
		</script>
	</head>

	<body>
		<div class="easyui-layout" data-options="fit:true,border:false">
			<div data-options="region:'center',border:false" >
				<table id="datagd"></table>
			</div>
		</div>
		<div id="toolbar" style="display:none;padding:5px;height:auto">
			<div>
				<sec:authorize ifAnyGranted='B_YHGL_ADD'>
					<a onclick="addUser();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a>
				</sec:authorize>
				<sec:authorize ifAnyGranted='B_YHGL_EDIT'>
					<a onclick="editUser();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">修改</a>
				</sec:authorize>
				<sec:authorize ifAnyGranted='B_YHGL_DEL'>
					<a onclick="delUser();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
				</sec:authorize>
				<sec:authorize ifAnyGranted='B_YHGL_AUTH'>
					<a onclick="openAuth();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">授权</a>
				</sec:authorize>
				<sec:authorize ifAnyGranted='B_YHGL_FPJS'>
					<a onclick="grantRole();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">分配角色</a>
				</sec:authorize>
			</div>
		</div>
		
		<div id="dlg" class="easyui-dialog" style="width:600px;height:auto;padding:10px 20px" buttons="#dlg-buttons" data-options="closed:true,modal:true">
			<form id="user_form" method="post">
				<input type="hidden" name="id" />
				<table class="grid">
					<tr>
		   				<td>用户名：</td>
		   				<td>
		   					<input type="text" id="userName" name="userName" class="easyui-textbox" data-options="required:true,validType:['username','length[0,20]']" />
		   				</td>
		   				<td>所属部门：</td>
		   				<td><input type="text" id="deptId" name="deptId" class="easyui-combotree" /></td>
		   			</tr>
					<tr>
						<td>密码：</td>
		 				<td>
		 					<input type="text" name="pwdBak" class="easyui-textbox" data-options="required:true" />
		 				</td>
		 				<td>员工编号：</td>
		 				<td>
		          			<input type="text" name="staffNo" class="easyui-textbox" />
		 				</td>
		 			</tr>
		   			<tr>
		   				<td>员工姓名：</td>
		   				<td>
							<input type="text" name="staffName" class="easyui-textbox" data-options="required:true,validType:['name','length[0,20]']" />
		   				</td>
		   				<td>性别：</td>
		   				<td>
							<select name="sex" class="easyui-combobox" data-options="width:100,editable:false,panelHeight:'auto'">
								<option value="0">男</option>
								<option value="1">女</option>
							</select>
		   				</td>
		   			</tr>
		   			<tr>
		   				<td>生日：</td>
		   				<td>
							<input type="text" name="birthDate" class="easyui-datebox" data-options="editable:false" />
		   				</td>
		   				<td>Email：</td>
		   				<td>
							<input type="text" name="email" class="easyui-textbox" data-options="validType:['email','length[0,40]']" />
		   				</td>
		   			</tr>
		   			<tr>
		   				<td>电话：</td>
		   				<td>
							<input type="text" name="telephone" class="easyui-textbox" data-options="validType:['phone','length[0,30]']" />
		   				</td>
		   				<td>手机：</td>
		   				<td>
							<input type="text" name="cellphone" class="easyui-textbox" data-options="validType:['mobile','length[0,30]']" />
		   				</td>
		   			</tr>
		   			<tr>
		   				<td>传真：</td>
		   				<td>
							<input type="text" name="fax" class="easyui-textbox" />
		   				</td>
		   				<td>职位：</td>
		   				<td>
							<input type="text" name="position" class="easyui-textbox" />
		   				</td>
		   			</tr>
		   			<tr>
		   				<td>QQ：</td>
		   				<td>
							<input type="text" name="qqChat" class="easyui-textbox" data-options="validType:['length[0,30]']" />
		   				</td>
		   				<td>证件号码：</td>
		   				<td>
							<input type="text" name="cardNo" class="easyui-textbox" data-options="validType:['idcard','length[0,40]']" />
		   				</td>
		   			</tr>
		   			<tr>
		   				<td>排序：</td>
		   				<td>
		   					<input type="text" name="orderNo" class="easyui-numberspinner" data-options="width:100,height:25,max:10000,min:0" />
		   				</td>
		   				<td>状态：</td>
		   				<td>
							<select name="status" class="easyui-combobox" data-options="width:100,editable:false,panelHeight:'auto'">
								<option value="0">正常</option>
								<option value="1">停用</option>
							</select>
		   				</td>
						</tr>
					<tr>
						<td>备注：</td>
						<td colspan="3">
							<input type="text" name="remark" class="easyui-textbox" data-options="multiline:true,height:50,width:300"/>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div id="dlg-buttons">
		   	<a href="javascript:void(0)" class="easyui-linkbutton c8" data-options="iconCls:'icon-ok'" onclick="javascript:saveUser();" style="width:90px;">保存</a>
		  	<a href="javascript:void(0)" class="easyui-linkbutton c2" data-options="iconCls:'icon-cancel'" onclick="javascript:$('#dlg').dialog('close');" style="width:90px;">取消</a>
		</div>
		
		<!-- 用户分配角色 -->
		<div id="roleDlg" class="easyui-dialog" style="width:550px;height:450px;" buttons="#roleDlg-buttons" data-options="closed:true,modal:true,resizable:true">
			<div class="easyui-layout" data-options="fit:true,border:false">
				<div data-options="region:'west',collapsible:false" title="可分配角色" style="width:227px;padding:5px;">
					<table id="selectRoles"></table> 
				</div>
				<div data-options="region:'center'" style="padding-top:120px;padding-left:1px;text-align:center">
					<a class="easyui-linkbutton" onclick="addRole();" style="width:40px;"> &gt;&gt; </a>
					<br/><br/><br/>
					<a class="easyui-linkbutton" onclick="removeRole();" style="width:40px;"> &lt;&lt; </a>
				</div>
				<div data-options="region:'east',collapsible:false" title="已分配角色" style="width:227px;padding:5px;">
					<table id="selectedRoles"></table> 
				</div>
			</div>
		</div>
		<div id="roleDlg-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c2" iconCls="icon-cancel" onclick="javascript:$('#roleDlg').dialog('close')" style="width:90px">关闭</a>
		</div>
		<!-- 用户分配角色 -->
		
	<body>

	<script type="text/javascript">
		//=========================用户增删改===============
		function saveUser(){
			var data = form2Json("user_form");
		    if($("#user_form").form('validate')){
		        $.post('save_update',{data:data},function(result) {
		            if(result.success){
		            	$('#dlg').dialog('close');
		                $.messager.show({
		                    title: '提示',
		                    msg: result.msg
		                });
		                $('#datagd').datagrid('reload');
		            }else{
		                $.messager.show({
		                    title: '提示',
		                    msg: result.msg
		                });
		            }
		        },'json');
		    }
		}
		
		function addUser() {
			$('#user_form').form('clear');
			$('#userName').textbox('readonly',false);
			$('#user_form').form('load',{
				sex:0,
				status:0,
				orderNo:0
			});
			$('#dlg').dialog('open').dialog('setTitle','新增用户');
		}
				
		function editUser(){
	        var row = $('#datagd').datagrid('getSelected');
	        $('#user_form').form('clear');
	        if (row){
	            $('#dlg').dialog('open').dialog('setTitle','修改用户');
	            $('#user_form').form('load',{
	            	id:row.id,
	            	deptId:row.dept_id,
	            	userName:row.user_name,
	            	pwdBak:row.pwd_bak,
	            	staffNo:row.staff_no,
	            	staffName:row.staff_name,
	            	sex:row.sex,
	            	birthDate:row.birth_date,
	            	email:row.email,
	            	telephone:row.telephone,
	            	cellphone:row.cellphone,
	            	fax:row.fax,
	            	position:row.position,
	            	qqChat:row.qq_chat,
	            	status:row.status,
	            	orderNo:row.order_no,
	            	cardNo:row.card_no,
	            	remark:row.remark
	            });
	            $('#userName').textbox('readonly',true);
	        } else {
	        	$.messager.alert('提示','请选择用户');
	        }
	    }
	    
		function delUser(){
			var row = $('#datagd').datagrid('getSelected');
			if (row){
				if(row.user_name == 'admin'){
					$.messager.alert('提示','admin账号不能删除');
				} else {
					$.messager.confirm('提示','请确认是否删除?',function(r){
						if (r){
							$.ajax({
								url:'delete',
								type:'post',
								dataType:'json',
								data:{id:row.id,name:row.user_name},
								success: function(result){
									if(result.success) {
										$.messager.show({
											title: '提示',
											msg: result.msg
										});
										$('#datagd').datagrid('reload');
										//删除完后easyui还存在数据缓存，并且是选中的，所以取消选中
										$('#datagd').datagrid('clearSelections');
									} else {
										$.messager.show({
											title: '提示',
											msg: result.msg
										});
									}
								}
							});
						}
					});
				}
			} else {
				$.messager.alert('提示','请选择用户','info');
			}
		}
		
		//================分配角色=====================
		//打开
		function grantRole(){
			var row = $('#datagd').datagrid('getSelected');
			if(row){
				$('#roleDlg').dialog('open').dialog('setTitle','分配角色');
				$("#selectRoles").datagrid({ 
					url:'no_roles?user_id='+row.id, 
			        width: 210, 
			        height: 'auto', 
			        border: false,
			        loadMsg:'正在加载数据...',
			        fit: true,				//自动大小 
			        idField:'id', 
			        rownumbers:true,//行号 
			        remoteSort:false,
			        frozenColumns:[[ 
						{field:'ck',checkbox:true} 
					]], 
			        columns:[[{
				        field:'role_name',
				        title:'角色名称',
				        sortable:true,
				        width:150,
				        align:'center'
					}]]
			    });
				$("#selectedRoles").datagrid({ 
					url:'roles?user_id='+row.id, 
			        width: 210, 
			        height: 'auto', 
			        border: false,
			        loadMsg:'正在加载数据...',
			        fit: true,				//自动大小 
			        idField:'id', 
			        rownumbers:true,//行号 
			        remoteSort:false,
			        frozenColumns:[[ 
						{field:'ck',checkbox:true} 
					]], 
			        columns:[[{
				        field:'role_name',
				        title:'角色名称',
				        sortable:true,
				        width:150,
				        align:'center'
					}]]
			    });
			} else {
				$.messager.alert('提示','请选择一条记录','info');
			}
		}
		//增加角色
		function addRole() {
			var row = $('#datagd').datagrid('getSelected');
			var checknodes = $('#selectRoles').datagrid('getChecked');
			var prams = [];
			if (checknodes && checknodes.length > 0) {
				for ( var i = 0; i < checknodes.length; i++) {
					var pram = new Object();
					pram.userId = row.id;
					pram.roleId = checknodes[i].id;
					prams.push(pram);
				}
				//转换成json字符串。
				var jsonText = JSON.stringify(prams);
				$.post('add_role', {data:jsonText}, function(result) {
					if(result.success){
						$('#selectRoles').datagrid('reload');
						$('#selectedRoles').datagrid('reload');
						//清除选中的状态，（虽然看不到选中，但获取选中行的时候还是会有）
						$('#selectRoles').datagrid('clearSelections');
						$('#selectedRoles').datagrid('clearSelections');
			        	$.messager.show({
		                    title: '提示',
		                    msg: result.msg
		                });
					}else{
						$.messager.show({
		                    title: '提示',
		                    msg: result.msg
		                });
					}
				}, 'json');
			}else{
				$.messager.alert('提示','请选择可分配的角色','info');
			}	
		}
		//移除角色
		function removeRole() {
			var row = $('#datagd').datagrid('getSelected');
			var checknodes = $('#selectedRoles').datagrid('getChecked');
			var prams = [];
			if (checknodes && checknodes.length > 0) {
				for ( var i = 0; i < checknodes.length; i++) {
					var pram = new Object();
					pram.userId = row.id;
					pram.roleId = checknodes[i].id;
					prams.push(pram);
				}
				//转换成json字符串。
				var jsonText = JSON.stringify(prams);
				$.post('remove_role', {data:jsonText}, function(result) {
					if(result.success){
						$('#selectRoles').datagrid('reload');
						$('#selectedRoles').datagrid('reload');
						//清除选中的状态，（虽然看不到选中，但获取选中行的时候还是会有）
						$('#selectRoles').datagrid('clearSelections');
						$('#selectedRoles').datagrid('clearSelections');
			        	$.messager.show({
		                    title: '提示',
		                    msg: result.msg
		                });
					}else{
						$.messager.show({
		                    title: '提示',
		                    msg: result.msg
		                });
					}
				}, 'json');
			}else{
				$.messager.alert('提示','请选择可移除的角色','info');
			}
		}
	</script>
<html>
